package de.muahd.saga.services.core.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@PropertySource(name="database", value="classpath:database.properties")
@ImportResource(value = { "classpath:springDataConfig.xml" })
public class DatabaseConfig {
	
	@Value("${driverClassName}")
	private String driverClassName;
	
	@Value("${url}")
	private String url;
	
	@Value("${user}")
	private String username;
	
	@Value("${password}")
	private String password;
	
	@Value("${hibernate.hbm2ddl.auto}")
	private String hbm2ddlAuto;
	
	@Bean
	public DataSource dataSource() {
		BasicDataSource basicDataSouce = new BasicDataSource();
		basicDataSouce.setDriverClassName(driverClassName);
		basicDataSouce.setUrl(url);
		basicDataSouce.setUsername(username);
		basicDataSouce.setPassword(password);
		return basicDataSouce;
	}

	@Bean
	public JpaTransactionManager transactionManager() {
		JpaTransactionManager hibernateTransactionManager = new JpaTransactionManager();
		hibernateTransactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
		return hibernateTransactionManager;
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean  entityManagerFactoryBean() {
		LocalContainerEntityManagerFactoryBean localSessionFactoryBean = new LocalContainerEntityManagerFactoryBean();
		localSessionFactoryBean.setPackagesToScan("de.muahd.saga");
		localSessionFactoryBean.setDataSource(dataSource());
		localSessionFactoryBean.setJpaVendorAdapter(hibernateVendor());

		Properties jpaProperties = new Properties();
		jpaProperties.setProperty("hibernate.hbm2ddl.auto", hbm2ddlAuto);
		jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
		
		localSessionFactoryBean.setJpaProperties(jpaProperties);
		
		return (LocalContainerEntityManagerFactoryBean ) localSessionFactoryBean;
	}

	@Bean
	public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
		PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor = new PersistenceExceptionTranslationPostProcessor();
		return persistenceExceptionTranslationPostProcessor;
	}
	
	@Bean
	public HibernateJpaVendorAdapter hibernateVendor() {
		HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
		return hibernateJpaVendorAdapter;
	}
}
